<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 加掩生成规则
 */

function createSalt($length)
{
    $key = '';
    $pattern = '123456789abcdefghijklmnpqrstuvwxyz';

    for($i=0; $i<$length; $i++)
    {
        $key .= $pattern{mt_rand(0,33)};    //生成php随机数
    }
    return $key;
}

/**
 * @name 与底层交互返回的方法
 * @param $code
 * @param string $msg
 * @param array $data
 * @Author wx
 * @date 2019/4/23 15:18
 */
function returnJson ($code = 200, $msg = '', $data = [])
{

    //服务器版本信息
    $version = config('server_version');

    $outData = [
        'code' => $code,
        'version' => $version,
        'message' => $msg,
    ];

    if (empty($data)) {
        $data = [];
    }

    if (isset($data['data']))
    {
        foreach ($data as $k=>$v)
        {
            $outData[$k] = $v;
        }
    }
    else
    {
        $outData['data'] = $data;
    }

    echo json_encode($outData);
    die;
}


/**
 * @name curl请求
 * @param $url
 * @param int $type
 * @param string $data
 * @return mixed
 * @Author wx
 * @date 2019/5/28 10:34
 */
function requestCurl($url, $type = 0, $data = ''){
    // 1. 初始化
    $ch = curl_init();

    // 2. 设置选项，包括URL
    curl_setopt($ch, CURLOPT_URL, $url);// CURLOPT_URL 指定请求的URL
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    //methon post or get
    if($type) {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }

    // 3. 执行并获取内容
    $output = curl_exec($ch);

    // 4. 释放curl句柄
    curl_close($ch);
    $jsoninfo = json_decode($output, true);
    return $jsoninfo;
}

/**
 * @name curl post request
 * @param $url
 * @param string $post_data
 * @param int $timeout
 * @return mixed
 * @author wx
 * @date 2020/7/13 15:51
 */
function curlPost($url, $post_data = '', $timeout = 5)
{
    // 1. 初始化
    $ch = curl_init();
    // 2. 设置选项，包括URL
    curl_setopt($ch, CURLOPT_URL, $url);// CURLOPT_URL 指定请求的URL

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt ($ch, CURLOPT_TIMEOUT, $timeout);

    // 3. 执行并获取内容
    $output = curl_exec($ch);
    // 4. 释放curl句柄
    curl_close($ch);

    return $output;
}


/**
 * @name 返回错误信息
 * @param $code
 * @param $msg
 * @author wx
 * @date 2019/11/27 10:55
 */
function returnErrJson($code, $msg)
{
    global $errCode;
    global $errMsg;

    $errCode = $code;
    $errMsg = $msg;

    $logSever = new \app\common\service\ServerOptionHistory();
    $logSever->addUserLog();
    $data['code'] = $code;
    $data['msg'] = $msg;

    echo json_encode($data, JSON_UNESCAPED_UNICODE); die;
}

/**
 * @name 获取文件后缀名
 * @param $filename
 * @return mixed
 * @author wx
 * @date 2019/11/29 11:27
 */
function getFileExt($filename)
{
    $arr = explode('.',$filename);
    return array_pop($arr);;
}

function getLimits(&$page, &$limit)
{
    $page =  !isset($_GET['page']) || !is_numeric($_GET['page']) || $_GET['page'] <= 1 ? 1 : $_GET['page'];
    $limit =  !isset($_GET['limit']) || !is_numeric($_GET['limit']) ? 10 : $_GET['limit'];

    $page--;

}
/**
 * @name 输出
 * @param $msg
 * @param $code
 * @author wx
 * @date 2020/7/1 11:19
 */
function jsonErrorOut($msg, $code, $data = '')
{
    global $errorMsg;
    global $errorCode;

    $errorMsg = $msg;
    $errorCode = $code;


    echo json_encode([
        'msg' => $msg,
        'code' => $code,
        'data' => $data
    ]);die;
}